#include "Light.h"

int Light::baseId;
Light::Light(void)
{
	diffuse[0]=diffuse[1]=diffuse[2] = 1.0;
	diffuse[3] = 1.0;
	ambient[0] = ambient[1] = 0.0;
	ambient[3] = 1.0f;
	ambient[2] = 1.0f;
	specular[0] = specular[1] = specular[2] = specular[3] = 1.0;
	culoare[0] = culoare[1] = culoare[2] = culoare[3]= 1.0;
	scalare[0] = scalare[1] = scalare[2] = 0.05;
	pozitie[0] = pozitie[1]=  pozitie[2]  = 0.0;
	diff =amb =spec = true;
	deseneaza = false;
	direction[1] = -0.5;
	pozitie[3] = 1.0f;
	Tip = Ideal;
	id = (baseId++)%8;
}
void Light::SetDiffuse(float r, float g, float b, float a)
{
	diffuse[0] = r;
	diffuse[1] = g;
	diffuse[2] = b;
	diffuse[3] = a;
};
void Light::SetAmbient(float r, float g, float b, float a)
{
	ambient[0] = r;
	ambient[1] = g;
	ambient[2] = b;
	ambient[3] = a;
};
void Light::SetSpecular(float r, float g, float b, float a)
{
	specular[0] = r;
	specular[1] = g;
	specular[2] = b;
	specular[3] = a;
};
void Light::SetDirection(float x, float y, float z)
{
	direction[0] = x;
	direction[1] = y;
	direction[2] = z;
};
void Light::SetDeseneaza(bool desen)
{
	deseneaza = desen;
};
void Light::SetPozitie(float x, float y, float z)
{
	pozitie[0] = x;
	pozitie[1] = y;
	pozitie[2] = z;
};

void Light::SetScalare(float x, float y, float z)
{
	scalare[0] = x;
	scalare[1] = y;
	scalare[2] = z;
};

void Light::SetCuloare(float r, float g, float b)
{
	culoare[0] = r;
	culoare[1] = g;
	culoare[2] = b;
};
void Light::SetTip(LuminaTip t)
{
	Tip = t;
};

void Light::Render()
{
	// atenuari standard
	glLightf(GL_LIGHT0 + id,GL_CONSTANT_ATTENUATION,1.0f);
	glLightf(GL_LIGHT0 + id,GL_LINEAR_ATTENUATION,0.2f);

	float a[4] = {0,0,0,1};
	// culoarea luminii 
	if (diff)
	glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, diffuse);
	else
		glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, a);
	// culoarea ambientala 
	if (amb)
	glLightfv(GL_LIGHT0 + id, GL_AMBIENT, ambient);
	else
		glLightfv(GL_LIGHT0 + id, GL_AMBIENT, a);
	// culoarea speculara
	if (spec)
	glLightfv(GL_LIGHT0 + id, GL_SPECULAR, specular);
	else
		glLightfv(GL_LIGHT0 + id, GL_SPECULAR, a);
	// pozitia luminii
	glLightfv(GL_LIGHT0 + id, GL_POSITION, pozitie);

	if (Tip == LuminaTip::Spot)
	{
		glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
		glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, 45.0);
		glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, 3.0);
	}

	// activam lumina
	glEnable(GL_LIGHT0 + id);

};
void Light::Draw()
{
	if (deseneaza)
	{
	glPushMatrix();

	// translatie
	
	glTranslatef(pozitie[0], pozitie[1],pozitie[2]);
	
	// scalare
	glScalef( scalare[0] , scalare[1] , scalare[2]);


	glColor3f(culoare[0],culoare[1],culoare[2]);
	float a[4] ={1.0f,1.0f,1.0f,1.0f};
	glutSolidSphere(1.0f, 50,50);
	glPopMatrix();
	}
};
void Light::Disable()
{
	glDisable(GL_LIGHT0 + id);
};

Light::~Light(void)
{
}
